<template>
  <div class="index-lists">
    <el-card class="!border-none" shadow="never">
      <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
        <el-form-item label="站点名称" prop="siteName">
          <el-input class="w-[280px]" v-model="queryParams.siteName"/>
        </el-form-item>
        <el-form-item label="上报时间" prop="reportingTime">
          <daterange-picker
              v-model:startTime="queryParams.createTimeStart"
              v-model:endTime="queryParams.createTimeEnd"
          />
        </el-form-item>
        <el-form-item label="处理状态" prop="omStatus">
          <el-select v-model="queryParams.omStatus" placeholder="请选择">
            <el-option label="未处理" value="-1"/>
            <el-option label="已完成" value="1"/>
          </el-select>
        </el-form-item>
        <el-form-item label="事件类型" prop="omStatus">
          <el-select v-model="queryParams.type" placeholder="请选择">
            <el-option label="运维上报" value="0"/>
            <el-option label="事件上报" value="1"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none mt-4" shadow="never">
      <div>
      </div>
      <el-table
          class="mt-4"
          size="large"
          v-loading="pager.loading"
          :data="pager.lists"
      >
        <el-table-column label="站点名称" prop="siteName" min-width="100"/>
        <el-table-column label="设备名称" prop="deviceName" min-width="100"/>
        <el-table-column label="上报时间" prop="reportingTime" min-width="100"/>
        <el-table-column label="管理员" prop="administrator" min-width="100"/>
        <el-table-column label="联系电话" prop="contactNumber" min-width="100"/>
<!--        <el-table-column label="详情描述" prop="details" min-width="100"/>-->
        <el-table-column label="详情图" prop="detailChart" min-width="120">
          <template #default="{ row }">
            <div v-if="row.detailChart && row.detailChart.length > 0">
              <img :src="row.detailChart[0]" style="width: 100px; height: 100px;" @click="openImage(row.detailChart[0])"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="事件类型" prop="type" min-width="100">
          <template #default="{ row }">
            {{ row.type === 0 ? '运维上报' : '事件上报' }}
          </template>
        </el-table-column>
        <el-table-column label="处理详情" prop="processingDetail" min-width="100"/>
        <el-table-column label="处理状态" prop="omStatus" min-width="100">
          <template #default="{ row }">
            {{ row.omStatus === -1 ? '未处理' : row.omStatus === 0 ? '正在处理' : '已完成' }}
          </template>
        </el-table-column>
        <el-table-column label="处理时间" prop="processingTime" min-width="100"/>
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="{ row }">
            <el-button
                v-perms="['record:edit']"
                type="primary"
                link
                v-if="row.omStatus !== 1"
                @click="handleEdit(row)"
            >
              去处理
            </el-button>
            <el-button
                v-perms="['record:edit1']"
                type="primary"
                link
                v-if="row.omStatus == 1"
                @click="handleEdit1(row)"
            >
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex justify-end mt-4">
        <pagination v-model="pager" @change="getLists"/>
      </div>
    </el-card>
    <edit-popup
        v-if="showEdit"
        ref="editRef"
        @success="getLists"
        @close="showEdit = false"
    />
    <EditPopup1
        v-if="showEdit1"
        ref="editRef1"
        @success="getLists"
        @close="showEdit1 = false"
    />
  </div>
</template>
<script lang="ts" setup name="record">
import {recordDelete, recordLists} from '@/api/record'
import {usePaging} from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import EditPopup1 from './edit1.vue'
import Viewer from 'viewerjs';

import 'viewerjs/dist/viewer.css'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const editRef1 = ref(null)
const showEdit = ref(false)
const showEdit1 = ref(false)
const queryParams = reactive({
  siteName: '',
  reportingTimeStart: '',
  reportingTimeEnd: '',
  omStatus: '', // 新增属性
  type:''
})

const {pager, getLists, resetPage, resetParams} = usePaging({
  fetchFun: recordLists,
  params: queryParams
})

const openImage = (imageUrl) => {
  const viewer = new Viewer(document.querySelector('.index-lists'), {
    toolbar: false, // 如果需要显示工具栏，则设置为true
    navbar: false, // 如果需要显示导航栏，则设置为true
    title: false // 如果需要显示图片标题，则设置为true
  });
  viewer.show(imageUrl);
};

// const handleAdd = async () => {
//   showEdit.value = true
//   await nextTick()
//   editRef.value?.open('add')
// }

const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.getDetail(data)
}
const handleEdit1 = async (data: any) => {
  console.log(editRef.value,"4545")
  showEdit1.value = true
  await nextTick()
  editRef1.value?.open('edit')
  editRef1.value?.getDetail(data)
}

const handleDelete = async (omId: number) => {
  await feedback.confirm('确定要删除？')
  await recordDelete({omId})
  feedback.msgSuccess('删除成功')
  getLists()
}

getLists()
</script>
